cd ""
use "data_analysis_final", clear

** Figure 1:
preserve 
* preparing the data
drop if year>2016

keep if red_t1_abs_disp!=.
keep country year red_* pdisp_* post_* pre_*

sort country year
bysort country: gen first=[_n]
replace first=0 if first!=1

gen nyear = -year
sort country nyear
bysort country: gen last=[_n]
replace last=0 if last!=1
drop nyear
sort country year

keep if first==1 | last==1

foreach v of varlist red_* pdisp_* post_* pre_*  {
g first_`v'=`v' if first==1
g last_`v'=`v' if last==1
bysort country: replace last_`v'=last_`v'[_n+1]
g d_`v'=last_`v'-first_`v'
g d_pct_`v'=(last_`v'-first_`v')/first_`v'*100
}
drop if first_red_m20_disp==.

keep country year d_pre_* d_pct_pre_* first_pre_* last_pre_*

* percentages reported in text in section "The Context"
su d_pre_p99p100 d_pct_pre_p99p100 d_pre_p40p60 d_pct_pre_p40p60 d_pre_p0p20 d_pct_pre_p0p20 

* percentages reported in text in section "The Context" for US
su d_pre_p99p100 d_pct_pre_p99p100 d_pre_p40p60 d_pct_pre_p40p60 first_pre_p40p60 last_pre_p40p60 d_pre_p0p20 d_pct_pre_p0p20 first_pre_p0p20 last_pre_p0p20 if country=="United States"


* creating the figure
graph hbar d_pre_p99p100  d_pre_p40p60  d_pre_p0p20, ///
over(country, label(labsize(medium)) axis(noline noextend)) ytitle("% of national income", color(gray))  nofill bar(1, bcolor(blue)) bar(2, bcolor(green)) bar(3, bcolor(red)) ///
legend(order(1 2 3) label(1 "Top 1%") label(2 "Middle 20%") label(3 "Bottom 20%")  placement(w) si(medium) region(lc(none) fc(none) ls(none)) keygap(*.5) colgap(*.5) symxs(*.5) row(3) position(6)) ///
   scheme(plotplain) ymtick(-.08(.02).08, labcolor(gray)) ylabel(-.08 "-8%" -.04 "-4%" 0 "0%" .04 "4%" .08 "8%", labcolor(gray) nogrid labsize(medium))  ///
   yline(0, lpattern(solid) lcolor(gray)) yscale(lcolor(gray)) graphregion(margin(0 0 0 0)) plotregion(margin(2 2 2 2)) xsize(7.2cm) ysize(9cm)
restore

** Figure 2
set scheme s1mono

g year2=year+40

*** redistribution with lump-sum benefits
* absolute transfer rate
twoway ///
(scatter red_t1_abs_disp year if year<2017, ms(o) mfc(blue%10) mlc(blue%0) yaxis(1)) ///
(scatter red_m20_abs_disp year if year<2017, ms(o) mfc(green%10) mlc(green%0) yaxis(1)) ///
(scatter red_b20_abs_disp year if year<2017, ms(o) mfc(red%10) mlc(red%0) yaxis(1))  ///
(lpoly red_t1_abs_disp year if year<2017, lcolor(blue) lwidth(thick) yaxis(1))  ///
(lpoly red_m20_abs_disp year if year<2017, lcolor(green) lwidth(thick) yaxis(1)) ///
(lpoly red_b20_abs_disp year if year<2017, lcolor(red) lwidth(thick) yaxis(1)) ///
(scatter red_t1_absls year2 if year2<2057, ms(t) mfc(blue%10) mlc(blue%0) yaxis(1)) ///
(scatter red_m20_absls year2 if year2<2057, ms(t) mfc(green%10) mlc(green%0) yaxis(1)) ///
(scatter red_b20_absls year2 if year2<2057, ms(t) mfc(red%10) mlc(red%0) yaxis(1))  ///
(lpoly red_t1_absls year2 if year2<2057, lcolor(blue) lwidth(thick) yaxis(1))  ///
(lpoly red_m20_absls year2 if year2<2057, lcolor(green) lwidth(thick) yaxis(1)) ///
(lpoly red_b20_absls year2 if year2<2057, lcolor(red) lwidth(thick) yaxis(1)), ///
scheme(s1mono) legend(order(4 5 6) label(4 "Top 1%") label(5 "Middle 20%") label(6 "Bottom 20%") placement(w) si(medium) region(lc(none) fc(none) ls(none)) keygap(*.5) colgap(*.5) symxs(*.5) row(3) position(9)) ///
ymtick(-.08(.02).08, labcolor(gray)) ///
 ylabel(none, value angle(horizontal) labsize(medium))  xline(2018, lcolor(gray)) ///
 ylabel(-.08 "-8%" -.06 "-6%" -.04 "-4%" -.02 "-2%" .02 "2%" .04 "4%" .06 "6%" .08 "8%", add custom labcolor(gray))  ///
 ylabel( 0 "{bf: 0%}", add custom labcolor(gray))  ///
xtitle("", size(medium) color(gray)) ytitle("% of national income", size(medium) color(gray)) title("Absolute Transfer Rates", size(medium)) xmtick(1980(10)2055) xlabel(1980 "1980" 1990 "1990" 2000 "2000" 2010 "2010" 2020 "1980" 2030 "1990"  2040 "2000"  2050 "2010" , angle(horizontal) labsize(medium) labcolor(gray)) ysc(r(-.08 .08) axis(1)) text(-0.08 1998 "{bf: excl. in-kind benefits}", size(small)) text(-0.08 2038 "{bf: incl. in-kind benefits}", size(small))

graph save "Fig_2a", replace

* relative transfer rate
twoway ///
(scatter red_t1_disp year if year<2017, ms(o) mfc(blue%10) mlc(blue%0)) ///
(scatter red_m20_disp year if year<2017, ms(o) mfc(green%10) mlc(green%0)) ///
(scatter red_b20_disp year if year<2017, ms(o) mfc(red%10) mlc(red%0))  ///
(lpoly red_t1_disp year if year<2017, lcolor(blue) lwidth(thick))  ///
(lpoly red_m20_disp year if year<2017, lcolor(green) lwidth(thick)) ///
(lpoly red_b20_disp year if year<2017, lcolor(red) lwidth(thick)) /// note that this is equivalent to omitting obs where transfers reduced Ls pre-tax income by more than 30 percent. 
(scatter red_t1ls year2 if year2<2057, ms(t) mfc(blue%10) mlc(blue%0)) ///
(scatter red_m20ls year2 if year2<2057, ms(t) mfc(green%10) mlc(green%0)) ///
(scatter red_b20ls year2 if year2<2057, ms(t) mfc(red%10) mlc(red%0))  ///
(lpoly red_t1ls year2 if year2<2057, lcolor(blue) lwidth(thick))  ///
(lpoly red_m20ls year2 if year2<2057, lcolor(green) lwidth(thick)) ///
(lpoly red_b20ls year2 if year2<2057, lcolor(red) lwidth(thick)), ///
scheme(s1mono) legend(order(4 5 6) label(4 "Top 1%") label(5 "Middle 20%") label(6 "Bottom 20%") placement(w) si(medium) region(lc(none) fc(none) ls(none)) keygap(*.5) colgap(*.5) symxs(*.5) row(3) position(9)) ///
 xline(2018, lcolor(gray)) ///
xtitle("", size(medium) color(gray)) ytitle("% of post-tax income", size(medium) color(gray)) ysc(titlegap(-1)) title("Relative Transfer Rates", size(medium)) xmtick(1980(10)2055) xlabel(1980 "1980" 1990 "1990" 2000 "2000" 2010 "2010" 2020 "1980" 2030 "1990"  2040 "2000"  2050 "2010" , angle(horizontal) labsize(medium) labcolor(gray)) ysc(r(-110 64))  ymtick(-140(20)80, ) ///
ylabel(none, value angle(horizontal) labsize(medium)) ///
ylab(-140 "-140%" -120 "-120%" -100 "-100%" -80 "-80%" -60 "-60%" -40 "-40%" -20 "-20%" 20 "20%" 40 "40%" 60 "60%" 80 "80%",  add labcolor(gray)) ///
ylab(0 "{bf: 0%}", add custom labcolor(gray)) ///
 text(-140 1998 "{bf: excl. in-kind benefits}", size(small)) text(-140 2038 "{bf: incl. in-kind benefits}", size(small))

graph save "Fig_2b", replace

grc1leg2 "Fig_2a" "Fig_2b",  labsize(small) position(6) lyoffset(4)
graph display, ysize(3.7) xsize(5.9)


* numbers reported in text in section "Redistribution Since 1980"
tab year, su(red_t1_abs_disp)
tab year, su(red_t1_absls)

tab year, su(red_m20_abs_disp)
tab year, su(red_m20_absls)

tab year, su(red_b20_abs_disp)
tab year, su(red_b20_absls)

tab year, su(red_t1ls)

tab year, su(red_m20_disp)

tab year, su(red_b20_disp)

**** Regressions **** 
* dropping the outliers in CH in 2008 and 2009 
drop if country=="Switzerland" & year>2007 & year<2010

* labeling
label var top_inequality "T1/M20"
label var bottom_inequality_b20 "M20/B20"
label var ikaopen "Capital market openness"
label var iopenc "Trade openness"
label var cum_left "Partisanship (left)"
label var imarriage_rate "Marriage rate"
label var unemp "Unemployment"
label var pb_ni "Public goods consumption"

****
xtset ccode year
****

* Table 1
xtpcse d1.red_t1_disp l1.red_t1_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp i.ccode, p 
outreg2 using regressions,  word  bd(2) sd(2) rd(2) alpha(0.05) symbol(*)  lab replace

xtpcse d1.red_t1ls l1.red_t1ls  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni i.ccode, p 
outreg2 using regressions,  word  bd(2) sd(2) rd(2) alpha(0.05) symbol(*)  lab append

xtpcse d1.red_m20_disp l1.red_m20_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp i.ccode, p 
outreg2 using regressions,  word  bd(2) sd(2) rd(2) alpha(0.05) symbol(*)  lab append

xtpcse d1.red_m20ls l1.red_m20ls d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc    d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni i.ccode, p  
outreg2 using regressions,  word  bd(2) sd(2) rd(2) alpha(0.05) symbol(*)  lab append

xtpcse d1.red_b20_disp l1.red_b20_disp  d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc  d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp i.ccode, p
outreg2 using regressions,  word  bd(2) sd(2) rd(2) alpha(0.05) symbol(*)  lab append

xtpcse d1.red_b20ls l1.red_b20ls d1.top_inequality l1.top_inequality d1.bottom_inequality_b20 l1.bottom_inequality_b20 d1.ikaopen l1.ikaopen d1.iopenc l1.iopenc    d1.cum_left l1.cum_left d1.imarriage_rate l1.imarriage_rate d1.unemp l1.unemp d1.pb_ni l1.pb_ni i.ccode, p 
outreg2 using regressions,  word  bd(2) sd(2) rd(2) alpha(0.05) symbol(*)  lab append



*** Long-run effects (figures 4 and 5)
use "data_analysis_final", clear

* dropping the outlier years in CH
drop if country=="Switzerland" & year>2007 & year<2010

* demeaning (by country) the variables
foreach v of varlist red_t1_disp red_t1ls red_t10_disp red_t10ls red_t20_disp red_t20ls red_m20_disp red_m20ls red_b20_disp red_b20ls top_inequality top_inequality_t10 top_inequality_t20 bottom_inequality_b20 ikaopen iopenc  cum_left  imarriage_rate pb_ni {
bysort country: egen mean_`v' = mean(`v')
g dm_`v' = .
replace dm_`v' = (`v'-mean_`v')
su dm_`v'
g std_`v'=`v'/`r(sd)'
}

****
xtset ccode year
****
tab ccode, g(c)
****

* Top 1% - excluding public goods
ivreg red_t1_disp d1.std_top_inequality std_top_inequality d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate d1.unemp unemp c2-c17 (d.red_t1_disp =  l1.red_t1_disp  std_top_inequality l1.std_top_inequality std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate unemp l1.unemp c2-c17)

matrix Hd=r(table)'
matrix list Hd

g top_inequality_pe3=Hd[3,1]
g top_inequality_ll3=Hd[3,5]
g top_inequality_hl3=Hd[3,6]

g bottom_inequality_pe3=Hd[5,1]
g bottom_inequality_ll3=Hd[5,5]
g bottom_inequality_hl3=Hd[5,6]

g cum_left_pe3=Hd[11,1]
g cum_left_ll3=Hd[11,5]
g cum_left_hl3=Hd[11,6]

* Top 1% - including public goods
ivreg red_t1ls d1.std_top_inequality std_top_inequality d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate d1.std_pb_ni std_pb_ni d1.unemp unemp c2-c17 (d.red_t1ls = l1.red_t1ls  std_top_inequality l1.std_top_inequality std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate std_pb_ni l1.std_pb_ni unemp l1.unemp c2-c17)

matrix H=r(table)'
matrix list H

g top_inequality_pe6=H[3,1]
g top_inequality_ll6=H[3,5]
g top_inequality_hl6=H[3,6]

g bottom_inequality_pe6=H[5,1]
g bottom_inequality_ll6=H[5,5]
g bottom_inequality_hl6=H[5,6]

g cum_left_pe6=H[11,1]
g cum_left_ll6=H[11,5]
g cum_left_hl6=H[11,6]

g pb_ni_pe6=H[15,1]
g pb_ni_ll6=H[15,5]
g pb_ni_hl6=H[15,6]

* Top 10% - excluding public goods
ivreg red_t10_disp d1.std_top_inequality_t10 std_top_inequality_t10 d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate d1.unemp unemp c2-c17 (d.red_t10_disp = l1.red_t10_disp  std_top_inequality_t10 l1.std_top_inequality_t10 std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate unemp l1.unemp c2-c17)

matrix Hdt10=r(table)'
matrix list Hdt10

g top_inequality_pe10=Hdt10[3,1]
g top_inequality_ll10=Hdt10[3,5]
g top_inequality_hl10=Hdt10[3,6]

g bottom_inequality_pe10=Hdt10[5,1]
g bottom_inequality_ll10=Hdt10[5,5]
g bottom_inequality_hl10=Hdt10[5,6]

g cum_left_pe10=Hdt10[11,1]
g cum_left_ll10=Hdt10[11,5]
g cum_left_hl10=Hdt10[11,6]

* Top 10% - including public goods
ivreg red_t10ls d1.std_top_inequality_t10 std_top_inequality_t10 d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate  d1.std_pb_ni std_pb_ni d1.unemp unemp c2-c17 (d.red_t10ls = l1.red_t10ls  std_top_inequality_t10 l1.std_top_inequality_t10 std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate std_pb_ni l1.std_pb_ni unemp l1.unemp c2-c17)

matrix Ht10=r(table)'
matrix list Ht10

g top_inequality_pe11=Ht10[3,1]
g top_inequality_ll11=Ht10[3,5]
g top_inequality_hl11=Ht10[3,6]

g bottom_inequality_pe11=Ht10[5,1]
g bottom_inequality_ll11=Ht10[5,5]
g bottom_inequality_hl11=Ht10[5,6]

g cum_left_pe11=Ht10[11,1]
g cum_left_ll11=Ht10[11,5]
g cum_left_hl11=Ht10[11,6]

g pb_ni_pe11=Ht10[15,1]
g pb_ni_ll11=Ht10[15,5]
g pb_ni_hl11=Ht10[15,6]


* Top 20% - excluding public goods
ivreg red_t20_disp d1.std_top_inequality_t20 std_top_inequality_t20 d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate d1.unemp unemp c2-c17 (d.red_t20_disp = l1.red_t20_disp  std_top_inequality_t20 l1.std_top_inequality_t20 std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate unemp l1.unemp c2-c17)

matrix Hdt20=r(table)'
matrix list Hdt20

g top_inequality_pe20=Hdt20[3,1]
g top_inequality_ll20=Hdt20[3,5]
g top_inequality_hl20=Hdt20[3,6]

g bottom_inequality_pe20=Hdt20[5,1]
g bottom_inequality_ll20=Hdt20[5,5]
g bottom_inequality_hl20=Hdt20[5,6]

g cum_left_pe20=Hdt20[11,1]
g cum_left_ll20=Hdt20[11,5]
g cum_left_hl20=Hdt20[11,6]

* Top 20% - including public goods
ivreg red_t20ls d1.std_top_inequality_t20 std_top_inequality_t20 d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate  d1.std_pb_ni std_pb_ni d1.unemp unemp c2-c17 (d.red_t20ls = l1.red_t20ls  std_top_inequality_t20 l1.std_top_inequality_t20 std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate std_pb_ni l1.std_pb_ni unemp l1.unemp c2-c17)

matrix Ht20=r(table)'
matrix list Ht20

g top_inequality_pe21=Ht20[3,1]
g top_inequality_ll21=Ht20[3,5]
g top_inequality_hl21=Ht20[3,6]

g bottom_inequality_pe21=Ht20[5,1]
g bottom_inequality_ll21=Ht20[5,5]
g bottom_inequality_hl21=Ht20[5,6]

g cum_left_pe21=Ht20[11,1]
g cum_left_ll21=Ht20[11,5]
g cum_left_hl21=Ht20[11,6]

g pb_ni_pe21=Ht20[15,1]
g pb_ni_ll21=Ht20[15,5]
g pb_ni_hl21=Ht20[15,6]


* Middle 20% - excluding public goods
ivreg red_m20_disp d1.std_top_inequality std_top_inequality d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate d1.unemp unemp c2-c17 (d.red_m20_disp = l1.red_m20_disp  std_top_inequality l1.std_top_inequality std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate unemp l1.unemp c2-c17)

matrix Md=r(table)'
matrix list Md

g top_inequality_pe2=Md[3,1]
g top_inequality_ll2=Md[3,5]
g top_inequality_hl2=Md[3,6]

g bottom_inequality_pe2=Md[5,1]
g bottom_inequality_ll2=Md[5,5]
g bottom_inequality_hl2=Md[5,6]

g cum_left_pe2=Md[11,1]
g cum_left_ll2=Md[11,5]
g cum_left_hl2=Md[11,6]

* Middle 20% - including public goods
ivreg red_m20ls d1.std_top_inequality std_top_inequality d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate d1.std_pb_ni std_pb_ni d1.unemp unemp c2-c17 (d.red_m20ls = l1.red_m20ls  std_top_inequality l1.std_top_inequality std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate std_pb_ni l1.std_pb_ni unemp l1.unemp c2-c17)

matrix M=r(table)'
matrix list M

g top_inequality_pe5=M[3,1]
g top_inequality_ll5=M[3,5]
g top_inequality_hl5=M[3,6]

g bottom_inequality_pe5=M[5,1]
g bottom_inequality_ll5=M[5,5]
g bottom_inequality_hl5=M[5,6]

g cum_left_pe5=M[11,1]
g cum_left_ll5=M[11,5]
g cum_left_hl5=M[11,6]

g pb_ni_pe5=M[15,1]
g pb_ni_ll5=M[15,5]
g pb_ni_hl5=M[15,6]


* Bottom 20% - excluding public goods
ivreg red_b20_disp d1.std_top_inequality std_top_inequality d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate d1.unemp unemp c2-c17 (d.red_b20_disp = l1.red_b20_disp  std_top_inequality l1.std_top_inequality std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate unemp l1.unemp c2-c17)

matrix Ld=r(table)'
matrix list Ld

g top_inequality_pe1=Ld[3,1]
g top_inequality_ll1=Ld[3,5]
g top_inequality_hl1=Ld[3,6]

g bottom_inequality_pe1=Ld[5,1]
g bottom_inequality_ll1=Ld[5,5]
g bottom_inequality_hl1=Ld[5,6]

g cum_left_pe1=Ld[11,1]
g cum_left_ll1=Ld[11,5]
g cum_left_hl1=Ld[11,6]

* Bottom 20% - including public goods
ivreg red_b20ls d1.std_top_inequality std_top_inequality d1.std_bottom_inequality_b20 std_bottom_inequality_b20 d1.ikaopen ikaopen d1.iopenc iopenc  d1.std_cum_left std_cum_left d1.imarriage_rate imarriage_rate d1.std_pb_ni std_pb_ni d1.unemp unemp c2-c17 (d.red_b20ls = l1.red_b20ls  std_top_inequality l1.std_top_inequality std_bottom_inequality_b20 l1.std_bottom_inequality_b20 ikaopen l1.ikaopen iopenc l1.iopenc  std_cum_left l1.std_cum_left imarriage_rate l1.imarriage_rate std_pb_ni l1.std_pb_ni unemp l1.unemp c2-c17)

matrix L=r(table)'
matrix list L

g top_inequality_pe4=L[3,1]
g top_inequality_ll4=L[3,5]
g top_inequality_hl4=L[3,6]

g bottom_inequality_pe4=L[5,1]
g bottom_inequality_ll4=L[5,5]
g bottom_inequality_hl4=L[5,6]

g cum_left_pe4=L[11,1]
g cum_left_ll4=L[11,5]
g cum_left_hl4=L[11,6]

g pb_ni_pe4=L[15,1]
g pb_ni_ll4=L[15,5]
g pb_ni_hl4=L[15,6]


* keeping the saved estimates
keep top_inequality* bottom_inequality* cum_left* pb_ni*
* dropping these variables so dataframe consists only of saved estimates
drop top_inequality top_inequality_t10 top_inequality_t20 bottom_inequality_b20 cum_left pb_ni
* deleting all the duplicates
duplicates drop top_inequality_pe2, force

* reformatting to long
g ind=1
reshape long top_inequality_pe top_inequality_ll top_inequality_hl bottom_inequality_pe bottom_inequality_ll bottom_inequality_hl cum_left_pe cum_left_ll cum_left_hl pb_ni_pe pb_ni_ll pb_ni_hl, i(ind) j(j)
replace ind=0 if j<4 | j==10 | j==20 // ind=0 for estimates that excludes public goods spending
replace j=j-3 if ind==1 // bottom 20%: j=1; middle 20%: j=2; top 1%: j=3
replace j=4 if j==8 | j==10 // top 10%: j=4
replace j=5 if j==18 | j==20 // top 20%: j=5
sort ind j

* switching placement of top 1% and top 20%
recode j (3=5) (5=3)
sort ind j

g j2=j
* add a little "jitter"
replace j=.90 if j==1 & ind==0
replace j=1.90 if j==2 & ind==0
replace j=2.90 if j==3 & ind==0
replace j=3.90 if j==4 & ind==0
replace j=4.90 if j==5 & ind==0

replace j=1.10 if j==1 & ind==1
replace j=2.10 if j==2 & ind==1
replace j=3.10 if j==3 & ind==1
replace j=4.10 if j==4 & ind==1
replace j=5.10 if j==5 & ind==1


* Figure 4
twoway rspike bottom_inequality_ll bottom_inequality_hl j if ind==0, scheme(plotplain) lcolor(gs11) lwidth(medthick) || ///
rspike bottom_inequality_ll bottom_inequality_hl j if ind==1,  lcolor(gs11)  lwidth(medthick) || ///
scatter bottom_inequality_pe  j if ind==0, msym(O) msize(medlarge) mlwidth(medthick) mlcolor("204 37 41") mfcolor("204 37 41") || ///
scatter bottom_inequality_pe  j  if ind==1, msym(O) msize(medlarge) mlwidth(medthick) mlcolor("62 150 81") mfcolor("62 150 81") xlabel(1 "Bottom 20%" 2 "Middle 20%" 3 "Top 20%" 4 "Top 10%" 5 "Top 1%", nogrid labsize(medlarge) labcolor(gray)) ylabel(-4(2)4,labsize(medlarge) labcolor(gray)) xscale(r(0.85 5.15)) yscale(r(-5.125 4)) xtitle("") title("Bottom-End Inequality", size(medlarge)) legend(order(3 4 ) label(3 "excl. in-kind transfers") label(4 "incl. in-kind transfers") placement(s) si(small) region(lc(none) fc(none) ls(none)) keygap(*.5) colgap(*.25) symxs(small) c(2) pos(6)) xsize(16) 

graph save "LRM_bottom", replace

twoway rspike top_inequality_ll top_inequality_hl j if ind==0, scheme(plotplain) lcolor(gs11) lwidth(medthick) || ///
rspike top_inequality_ll top_inequality_hl j if ind==1,  lcolor(gs11)  lwidth(medthick) || ///
scatter top_inequality_pe j if ind==0, msym(O) msize(medlarge) mlwidth(medthick) mlcolor("204 37 41") mfcolor("204 37 41") || ///
scatter top_inequality_pe j  if ind==1, msym(O) msize(medlarge) mlwidth(medthick) mlcolor("62 150 81") mfcolor("62 150 81") xlabel(1 "Bottom 20%" 2 "Middle 20%" 3 "Top 20%" 4 "Top 10%" 5 "Top 1%", nogrid labsize(medlarge) labcolor(gray)) xscale(r(0.85 5.15)) ylabel(-4(2)4,labsize(medlarge) labcolor(gray)) yscale(r(-5.125 4)) xtitle("") title("Top-End Inequality", size(medlarge)) legend(order(3 4 ) label(3 "excl. in-kind transfers") label(4 "incl. in-kind transfers") placement(s) si(small) region(lc(none) fc(none) ls(none)) keygap(*.5) colgap(*.25) symxs(small) c(2) pos(6)) xsize(16) 

graph save "LRM_top", replace

grc1leg2  "LRM_top" "LRM_bottom", scheme(s1mono) c(1) lyoffset(3)
graph display, ysize(3.7) xsize(5.9)

* fictive pe to get the two subfigures aligned.
g fic_pe=1

* Figure 5
twoway rspike cum_left_ll cum_left_hl j if ind==0, scheme(plotplain) lcolor(gs11) lwidth(medthick) || ///
rspike cum_left_ll cum_left_hl j if ind==1,  lcolor(gs11)  lwidth(medthick) || ///
scatter cum_left_pe  j if ind==0, msym(O) msize(medlarge) mlwidth(medthick) mlcolor("204 37 41") mfcolor("204 37 41") || ///
scatter cum_left_pe  j  if ind==1, msym(O) msize(medlarge) mlwidth(medthick) mlcolor("62 150 81") mfcolor("62 150 81") xlabel(1 "Bottom 20%" 2 "Middle 20%" 3 "Top 20%" 4 "Top 10%" 5 "Top 1%", nogrid labsize(medlarge) labcolor(gray)) ylabel(-6(2)4,labsize(medlarge) labcolor(gray)) xscale(r(0.85 5.15)) yscale(r(-7 4)) xtitle("") title("Cumulative Left Partisanship", size(medlarge)) legend(order(3 4 ) label(3 "excl. in-kind transfers") label(4 "incl. in-kind transfers") placement(s) si(small) region(lc(none) fc(none) ls(none)) keygap(*.5) colgap(*.25) symxs(small) c(2) pos(6)) xsize(16) 

graph save "LRM_party", replace

twoway rspike pb_ni_ll pb_ni_hl j2 if ind==1, scheme(plotplain)  lcolor(gs11)  lwidth(medthick) || ///
scatter fic_pe  j if ind==0, msym(O) msize(medlarge) mlwidth(medthick) mlcolor("white") mfcolor("white") || ///
scatter fic_pe  j if ind==1, msym(O) msize(medlarge) mlwidth(medthick) mlcolor("white") mfcolor("white") || ///
scatter pb_ni_pe  j2  if ind==1, msym(O) msize(medlarge) mlwidth(medthick) mlcolor("62 150 81") mfcolor("62 150 81") xlabel(1 "Bottom 20%" 2 "Middle 20%" 3 "Top 20%" 4 "Top 10%" 5 "Top 1%", nogrid labsize(medlarge) labcolor(gray)) ylabel(-6(2)4,labsize(medlarge) labcolor(gray)) xscale(r(0.85 5.15)) yscale(r(-7 4)) xtitle("") title("Public Goods Consumption", size(medlarge))legend(order(3 4 ) label(3 "excl. in-kind transfers") label(4 "incl. in-kind transfers") placement(s) si(small) region(lc(none) fc(none) ls(none)) keygap(*.5) colgap(*.25) symxs(small) c(2) pos(6)) xsize(16) 

graph save "LRM_public_goods", replace

grc1leg2  "LRM_party" "LRM_public_goods", scheme(s1mono) c(1) lyoffset(3)
graph display, ysize(3.7) xsize(5.9)


*** Income growth (figure 3) ***

** First, we calculate the average for Europe, exclusive of Belgium and Austria, with base 100=1982
use "data_analysis_final", clear

drop if alspost_p0p20==.

* defining the Europe sample
g full_sample_eu=1 if country!="Belgium" & country!="United States" & country!="Austria"

* defining the year sample
drop if full_sample_eu!=1 | year>2016 | year<1982

* time setting the data
drop ccode
encode country, g(ccode)
xtset ccode year
 
* calculating inceom growth the base 100=1982
* Real extended income growth
sort country year
bysort country: gen index_ext_b20 = 100*apost_p0p20/apost_p0p20[1] // baseline growth rate; base 100 = 1982
bysort country: gen index_ext_b20ls = 100*alspost_p0p20/alspost_p0p20[1] // baseline growth rate; base 100 = 1982

bysort country: gen index_ext_m20 = 100*apost_p40p60/apost_p40p60[1] // baseline growth rate; base 100 = 1982
bysort country: gen index_ext_m20ls = 100*alspost_p40p60/alspost_p40p60[1] // baseline growth rate; base 100 = 1982

bysort country: gen index_ext_mean = 100*apost_p0p100/apost_p0p100[1] // baseline growth rate; base 100 = 1982

sort year
 
 bysort year: egen eu_index_ext_mean=mean(index_ext_mean)
 
 bysort year: egen eu_index_ext_m20=mean(index_ext_m20)
 bysort year: egen eu_index_ext_m20ls=mean(index_ext_m20ls)
 
 bysort year: egen eu_index_ext_b20=mean(index_ext_b20) 
 bysort year: egen eu_index_ext_b20ls=mean(index_ext_b20ls) 
 
 keep country year eu_index*
 
 duplicates drop year, force
 
 sort year
 
 replace country="Europe"
 
 ren eu_index_ext_mean index_ext_mean
 
 ren eu_index_ext_m20 index_ext_m20
 ren eu_index_ext_m20ls index_ext_m20ls
 
 ren eu_index_ext_b20 index_ext_b20
 ren eu_index_ext_b20ls index_ext_b20ls
 save "real_ext_income_eu14", replace

  
** Then, we calculate growth rates for all countries and merge it with the growht rates for Europe as a whole.
use "data_analysis_final", clear

drop if alspost_p0p20==.

drop ccode
encode country, g(ccode)
xtset ccode year

* Real extended income growth
* bottom 20%
sort country year
bysort country: gen index_ext_b20 = 100*apost_p0p20/apost_p0p20[1] 
bysort country: gen index_ext_b20ls = 100*alspost_p0p20/alspost_p0p20[1] 

* middle 20%
bysort country: gen index_ext_m20 = 100*apost_p40p60/apost_p40p60[1] 
bysort country: gen index_ext_m20ls = 100*alspost_p40p60/alspost_p40p60[1] 

* overall average
bysort country: gen index_ext_mean = 100*apost_p0p100/apost_p0p100[1] 

keep country year index*

* merging with the growth rates for Europe as a whole
 append using "real_ext_income_eu14"

 encode country, g(c)
 tab c
 
* rearranging so that Europe will show up last next to US.  
 recode c (1=5 "Austria") (2=6 "Belgium") (3=1 "Denmark") (4=18 "Europe") (5=2 "Finland") (6=7 "France") (7=8 "Germany") (8=11 "Greece") ///
 (9=15 "Ireland") (10=12 "Italy") (11=10 "Netherlands") (12=3 "Norway") (13=13 "Portugal") (14=14 "Spain") (15=4 "Sweden") (16=9 "Switzerland") ///
 (17=16 "UK") (18=17 "US"), g(c2)


* setting up the figures
grstyle init
grstyle set plain, horizontal grid
grstyle set margin "0 0 0 0", cm: graph
grstyle set linewidth 0pt: axisline tick legend xyline
grstyle set legend 9, nobox


* Figure 3
twoway ///
(line index_ext_mean year, by(c2, r(2) im(.5 .5 1 1) note(""))   lcolor(blue) lpattern(solid)) ///
(line index_ext_m20ls year,   lcolor(green) lpattern(solid)) ///
(line index_ext_b20ls year,  lcolor(red) lpattern(solid)), ///
ylabel(100(50)250, angle(horizontal) labsize(medium) labcolor(gray)) ysc(r(75 250)) ///
xtitle("", size(medium)) ytitle("Post-tax income (base 100=1980)", size(vsmall) color(gray)) xlabel(1980 " " 1990 "1990" 2000 " " 2010 "2010" 2020 " ", angle(horizontal) labsize(medium) labcolor(gray)) subtitle(,size(large)) ///
legend(order(1 "Overall average" 2 "Middle 20%" 3 "Bottom 20%") placement(w) si(vsmall) region(lc(none) ///
   fc(none) ls(none)) keygap(*.3) colgap(*.3) symxs(*.3) row(3)) xtitle("") graphregion(margin(0 0 0 0)) plotregion(margin(2 2 2 2)) xsize(16cm) ysize(10cm)

 
 * Figure D6 (only spending health is distributed as an equal lump sum)
 twoway ///
(line index_ext_mean year, by(c2, r(2) im(.5 .5 1 1) note(""))   lcolor(blue) lpattern(solid)) ///
(line index_ext_m20 year,   lcolor(green) lpattern(solid)) ///
(line index_ext_b20 year,  lcolor(red) lpattern(solid)), ///
ylabel(50(50)250, angle(horizontal) labsize(medium) labcolor(gray)) ysc(r(50 250)) ///
xtitle("", size(medium)) ytitle("Post-tax income (base 100=1980)", size(small)) xlabel(1980 " " 1990 "1990" 2000 " " 2010 "2010" 2020 " ", angle(horizontal) labsize(medium) labcolor(gray)) subtitle(,size(large)) ///
legend(order(1 "Overall average" 2 "Middle 20%" 3 "Bottom 20%") placement(w) si(vsmall) region(lc(none) ///
   fc(none) ls(none)) keygap(*.3) colgap(*.3) symxs(*.3) row(3)) xtitle("") graphregion(margin(0 0 0 0)) plotregion(margin(2 2 2 2)) xsize(16cm) ysize(10cm)
